package aom.ju.ss.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.io.ByteArrayOutputStream;

import aom.ju.ss.BuildConfig;
import aom.ju.ss.R;
import aom.ju.ss.mode.LpKey;
import aom.ju.ss.mode.UserInfo;


public class ShareManager {

    /**
     * 自定义分享界面
     */
    public static void showShare(Context context, String platform) {

        String apis = BuildConfig.DEBUG ? "4" : "3";
        IWXAPI api = WXAPIFactory.createWXAPI(context, LpKey.INSTANCE.getWX_APP_ID(), true);
        api.registerApp(LpKey.INSTANCE.getWX_APP_ID());
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = "api" + apis + ".hcjuquan.com/index/share/login?token=" + UserInfo.Companion.getToken();

        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = "我在聚刷刷APP体验砍价，每月玩转1500元！注册送2张砍价券！ ";
        msg.description = "惊喜连连，好处多多，赶紧一起来玩吧！";

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BitmapFactory.decodeResource(context.getResources(), R.drawable.logo_mini)
                .compress(Bitmap.CompressFormat.PNG, 100, baos);
        msg.thumbData = baos.toByteArray();

        //发送请求
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        //创建唯一标识
        req.transaction = System.currentTimeMillis() + "";
        req.message = msg;
        req.scene = platform.equals("Wechat") ? SendMessageToWX.Req.WXSceneSession : SendMessageToWX.Req.WXSceneTimeline;
        api.sendReq(req);
    }

    public static void sharePic(Context context, String path, boolean ishy) {
        IWXAPI api = WXAPIFactory.createWXAPI(context, LpKey.INSTANCE.getWX_APP_ID(), true);
        api.registerApp(LpKey.INSTANCE.getWX_APP_ID());
        WXImageObject fileObj = new WXImageObject();
        fileObj.imagePath = path;
        //使用媒体消息分享
        WXMediaMessage msg = new WXMediaMessage(fileObj);
        msg.title = " ";
        //发送请求
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        //创建唯一标识
        req.transaction = System.currentTimeMillis() + "";
        req.message = msg;
        req.scene = ishy ? SendMessageToWX.Req.WXSceneSession : SendMessageToWX.Req.WXSceneTimeline;
        api.sendReq(req);
    }

    /**
     * 自定义分享界面
     */
    public static void showShareProduct(Activity context, String platform, String mTitle, String mText, String mTitleUrl, String mImageUrl) {


        IWXAPI api = WXAPIFactory.createWXAPI(context, LpKey.INSTANCE.getWX_APP_ID(), true);
        api.registerApp(LpKey.INSTANCE.getWX_APP_ID());
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = mTitleUrl;

        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = mTitle;
        msg.description = mText;

        LogUtils.e(mImageUrl);

        Glide.with(context)
                .asBitmap()
                .load(mImageUrl)
                .override(72)
                .error(R.drawable.logo_mini)
                .into(new CustomTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        LogUtils.e("11111111");
                        context.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                SendMessageToWX.Req req = new SendMessageToWX.Req();
                                req.transaction = System.currentTimeMillis() + "";
                                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                                resource.compress(Bitmap.CompressFormat.PNG, 100, baos);
                                resource.recycle();//自由选择是否进行回收
                                msg.thumbData = baos.toByteArray();
                                req.message = msg;
                                req.scene = platform.equals("Wechat") ? SendMessageToWX.Req.WXSceneSession : SendMessageToWX.Req.WXSceneTimeline;
                                api.sendReq(req);

                            }
                        });

                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {

                    }
                });

    }

    /**
     * 自定义分享界面
     */
    public static void showSharePoster(Context context, String platform, String filePath) {

        sharePic(context, filePath, platform.equals("Wechat"));
    }

}
